<template>
  <div class="app-container">
    <el-row style="margin-bottom: 24px">
      <el-col :span="6"
      >
        <div class="flex flex-column align-center">
          <div class="title">房间总数</div>
          <div class="total">{{ Statistics.totalHouseNumber }}间</div>
        </div>
      </el-col
      >
      <el-col :span="6"
      >
        <div class="flex flex-column align-center left-border right-border">
          <div class="title">已出租总数</div>
          <div class="total">{{ Statistics.rentalHouseNumber }}间</div>
        </div>
      </el-col
      >
      <el-col :span="6"
      >
        <div class="flex flex-column align-center right-border">
          <div class="title">空置总数</div>
          <div class="total">{{ Statistics.vacancyHouseNumber }}间</div>
        </div>
      </el-col
      >
      <el-col :span="6"
      >
        <div class="flex flex-column align-center">
          <div class="title">空置率</div>
          <div class="total">{{ Statistics.vacancyHousePercentage }}</div>
        </div>
      </el-col
      >
    </el-row>
    <el-row style="margin-bottom: 24px">
      <el-col :span="6"
      >
        <div class="flex flex-column align-center">
          <div class="title">线下可出租总数</div>
          <div class="total">{{ Statistics.onlineDownHouseNumber }}间</div>
        </div>
      </el-col
      >
      <el-col :span="6"
      >
        <div class="flex flex-column align-center left-border right-border">
          <div class="title">线下已出租总数</div>
          <div class="total">
            {{ Statistics.onlineDownRentalHouseNumber }}间
          </div>
        </div>
      </el-col
      >
      <el-col :span="6"
      >
        <div class="flex flex-column align-center right-border">
          <div class="title">线上可出租总数</div>
          <div class="total">{{ Statistics.onlineUpHouseNumber }}间</div>
        </div>
      </el-col
      >
      <el-col :span="6"
      >
        <div class="flex flex-column align-center">
          <div class="title">线上已出租总数</div>
          <div class="total">{{ Statistics.onlineUpRentalHouseNumber }}间</div>
        </div>
      </el-col
      >
    </el-row>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="房间" prop="roomNumber">
        <el-input
          v-model="queryParams.roomNumber"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋管理员" prop="man">
        <el-input
          v-model="queryParams.man"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋类型" prop="houseNature">
        <el-select
          v-model="queryParams.houseNature"
          placeholder=""
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.dict_house_nature"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新建
        </el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-radio-group v-model="current" @input="stateHandel">
        <el-radio-button :label="index" v-for="(item, index) in countHouseList" :key="index"
        >{{ item.label }}（{{ item.number }}）
        </el-radio-button
        >
      </el-radio-group>
    </el-row>
    <div class="info">
      <i class="el-icon-info"></i> <span>已选择</span> {{ ids.length }}
      <span>项</span> <label @click="clearSelect" class="clear">清空</label>
    </div>

    <el-table
      v-loading="loading"
      :data="roomList"
      @selection-change="handleSelectionChange"
      ref="table"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="房间编号" prop="houseId" width="120" /> -->
      <el-table-column
        label="房间"
        prop="roomNumber"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="楼盘"
        prop="estateName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="地区"
        prop="provinceName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="房屋类型" prop="houseNature">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.dict_house_nature"
            :value="scope.row.houseNature"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否线上出租" prop="roleSort">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.dict_online_rental_type"
            :value="scope.row.onlineRentalType"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="roleSort">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.dict_house_state"
            :value="scope.row.houseState"
          />
        </template>
      </el-table-column>
      <el-table-column label="承租人" prop="roleSort"/>
      <el-table-column label="房屋管理员" prop="houseManagerName"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >详情
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 新增房间弹窗 -->
    <room-add :open="open" v-if="open" @closeeldialog="diaclose" :roomId="roomId"></room-add>
    <!-- 详情弹窗 -->
    <room-detail
      :open.sync="detailDialogVisible"
      v-if="detailDialogVisible"
      :roomId="roomId"
    ></room-detail>
  </div>
</template>

<script>
import {
  listRoom,
  getRoom,
  getStatistics,
  countHouseState,
  delRoom
} from '@/api/house/room'

import {
  addRole,
  updateRole,
  dataScope,
  deptTreeSelect
} from '@/api/system/role'
import roomAdd from './component/room-add.vue'
import roomDetail from './component/room-detail.vue'

export default {
  name: 'roomManage',
  dicts: ['dict_online_rental_type', 'dict_house_state', 'dict_house_nature'],
  components: {
    roomAdd,
    roomDetail
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      roomList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 当前选择
      current: '0',
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],

      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleKey: undefined,
        roomNumber: undefined,
        houseNature: undefined,
        houseState: undefined,
        chargeType: undefined
      },
      countHouseList: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 表单校验
      rules: {
        roomNumber: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        roleKey: [
          { required: true, message: '权限字符不能为空', trigger: 'blur' }
        ],
        roleSort: [
          { required: true, message: '角色顺序不能为空', trigger: 'blur' }
        ]
      },
      Statistics: {
        totalHouseNumber: 3, //房间总数
        rentalHouseNumber: 2, //已出租房间总数
        vacancyHouseNumber: 1, //空置房间总数
        vacancyHousePercentage: '33.33%', //空置房间的比率
        onlineDownHouseNumber: 2, //线下房间总数
        onlineDownRentalHouseNumber: 1, //线下出租房间总数
        onlineUpHouseNumber: 1, //线上房间总数
        onlineUpRentalHouseNumber: 1 //线上出租房间总数
      },
      detailDialogVisible: false,
      roomId: ''
    }
  },
  created() {
    this.getList()
    this.getStatistics()
  },
  activated() {
    this.getList()
    this.getStatistics()
  },
  methods: {
    stateHandel(i) {
      delete this.queryParams['houseLeaseState']
      delete this.queryParams['houseState']
      let value = this.countHouseList[i].value
      let type = this.countHouseList[i].type
      if (value && type) {
        this.queryParams[type] = value
      } else {
        this.queryParams['chargeType'] = ''
        this.queryParams['houseState'] = ''
      }
      this.handleQuery()

    },
    diaclose() {
      this.open = false
      this.getList()
      this.getStatistics()
    },
    /** 查询房间列表 */
    getList() {
      this.loading = true
      this.queryParams.startTime = this.dateRange[0]
      this.queryParams.endTime = this.dateRange[1]
      listRoom(this.queryParams).then(
        (response) => {
          this.roomList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
      countHouseState().then((response) => {
        this.countHouseList = response.data
      })
    },
    /** 查询统计房间数据 */
    getStatistics() {
      getStatistics().then((response) => {
        this.Statistics = response.data
      })
    },

    /** 根据角色ID查询部门树结构 */
    getDeptTree(roleId) {
      return deptTreeSelect(roleId).then((response) => {
        this.deptOptions = response.depts
        return response
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.houseId)
      this.single = selection.length != 1
      this.multiple = !selection.length
      this.queryParams.houseIds = this.ids
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.roomId = ''
      this.open = true
    },
    // 详情操作
    handleDetail(row) {
      this.roomId = row.houseId
      this.detailDialogVisible = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.roomId = row.houseId
      this.open = true
    },

    /** 选择角色权限范围触发 */
    dataScopeSelectChange(value) {
      if (value !== '2') {
        this.$refs.dept.setCheckedKeys([])
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.roleId != undefined) {
            updateRole(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRole(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const houseId = row.houseId || this.ids
      this.$modal
        .confirm('是否确认删除编号为"' + houseId + '"的房间？')
        .then(function() {
          return delRoom(houseId)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      console.log(this.queryParams)
      this.download(
        'enterprise/house/house/export',
        {
          ...this.queryParams
        },
        `房间_${new Date().getTime()}.xlsx`
      )
    },
    //请空选择项
    clearSelect() {
      this.$refs.table.clearSelection()
    }
  }
}
</script>
<style lang="scss" scoped>
.title {
  font-size: 14px;
  font-weight: 500;
  color: #666;
}

.total {
  font-size: 18px;
  font-weight: 500;
  color: #333;
  margin-top: 6px;
}

.left-border {
  border-left: 1px solid #999;
}

.right-border {
  border-right: 1px solid #999;
}

.info {
  padding: 10px 15px;
  margin: 15px 0;
  background: #e6f7ff;
  color: #1890ff;

  span {
    color: #565c5f;
  }

  .clear {
    margin-left: 20px;
    cursor: pointer;
  }
}
</style>
